$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'base/limitspeed/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '左线/右线', name: 'linename', index: 'linename', width: 80 }, 			
			{ label: '开始点', name: 'beginpoint', index: 'beginpoint', width: 80 }, 			
			{ label: '终止点', name: 'endpoint', index: 'endpoint', width: 80 }, 			
			{ label: '0限速 1禁止', name: 'type', index: 'type', width: 80 }, 			
			{ label: '限速|限行 米/秒', name: 'memo', index: 'memo', width: 80 }, 			
			{ label: '时间', name: 'createtime', index: 'createtime', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		limitspeed: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.limitspeed = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
//			console.log($("#limitspeed_linename").val())
//			console.log($("#beginpoint_dk").val())
//			console.log($("#beginpoint_dk_s").val())
//			console.log($("#endpoint_dk").val())
//			console.log($("#endpoint_dk_s").val())
//			console.log($("#limitspeed_type").val())
			
			vm.limitspeed.linename = $("#limitspeed_linename").val();
			vm.limitspeed.beginpoint = parseInt($("#beginpoint_dk").val()) + parseInt($("#beginpoint_dk_s").val())
			vm.limitspeed.endpoint = parseInt($("#endpoint_dk").val()) + parseInt($("#endpoint_dk_s").val())
			vm.limitspeed.type = $("#limitspeed_type").val();
			
			
			var url = vm.limitspeed.id == null ? "base/limitspeed/save" : "base/limitspeed/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.limitspeed),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "base/limitspeed/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "base/limitspeed/info/"+id, function(r){
                vm.limitspeed = r.limitspeed;
                $("#limitspeed_linename option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            //console.log(optionVal +" " + optionText);
		            if(optionVal == vm.limitspeed.linename )
		            {
		            	$("#limitspeed_linename").val(optionVal)
		            }
		        });
                
                $("#beginpoint_dk option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            //console.log(parseInt(parseInt(vm.limitspeed.beginpoint)/1000)*1000 )
		            //console.log(optionVal +" " + optionText+ ' ' +parseInt(parseInt(vm.limitspeed.beginpoint)/1000)*1000);
		            if(parseInt(optionVal) == parseInt(parseInt(vm.limitspeed.beginpoint)/1000)*1000 )
		            {
		            	$("#beginpoint_dk").val(optionVal)
		            }
		        });
                
                $("#beginpoint_dk_s option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            //console.log(parseInt(parseInt(vm.limitspeed.beginpoint)/1000)*1000 )
		            //console.log(optionVal +" " + optionText+ ' ' + parseInt(vm.limitspeed.beginpoint)%1000);
		            if(parseInt(optionVal) == parseInt(vm.limitspeed.beginpoint)%1000 )
		            {
		            	$("#beginpoint_dk_s").val(optionVal)
		            }
		        });
                
                $("#endpoint_dk option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            if(parseInt(optionVal) == parseInt(parseInt(vm.limitspeed.endpoint)/1000)*1000 )
		            {
		            	$("#endpoint_dk").val(optionVal)
		            }
		        });
                
                $("#endpoint_dk_s option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            if(parseInt(optionVal) == parseInt(vm.limitspeed.endpoint)%1000 )
		            {
		            	$("#endpoint_dk_s").val(optionVal)
		            }
		        });
                
                $("#limitspeed_type option").each(function () {
		            var optionVal = $(this).val();
		            var optionText = $(this).text();
		            if(parseInt(optionVal) == parseInt(vm.limitspeed.type) )
		            {
		            	$("#limitspeed_type").val(optionVal)
		            }
		        });
                
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});